const user = {
    state: {
        userId: "", // 用户id
        username: "", // 用户账号
        avator: "", // 用户头像
        introduction: "", // 用户签名
    },
    getters: {
        userId: (state) => {
            let userId = state.userId;
            if (!userId) {
                userId = JSON.parse(window.sessionStorage.getItem("userId"));
            }
            return userId;
        },
        username: (state) => {
            let username = state.username;
            if (!username) {
                username = JSON.parse(
                    window.sessionStorage.getItem("username"),
                );
            }
            return username;
        },
        avator: (state) => {
            let avator = state.avator;
            if (!avator) {
                avator = JSON.parse(window.sessionStorage.getItem("avator"));
            }
            return avator;
        },
        introduction: (state) => {
            let introduction = state.introduction;
            if (!introduction) {
                introduction = JSON.parse(
                    window.sessionStorage.getItem("introduction"),
                );
            }
            return introduction;
        },
    },
    mutations: {
        setUserId: (state, userId) => {
            state.userId = userId;
            window.sessionStorage.setItem("userId", JSON.stringify(userId));
        },
        setUsername: (state, username) => {
            state.username = username;
            window.sessionStorage.setItem("username", JSON.stringify(username));
        },
        setAvator: (state, avator) => {
            state.avator = avator;
            window.sessionStorage.setItem("avator", JSON.stringify(avator));
        },
        setIntroduction: (state, introduction) => {
            state.introduction = introduction;
            window.sessionStorage.setItem(
                "introduction",
                JSON.stringify(introduction),
            );
        },
    },
};

export default user;
